# -*- coding: UTF-8 -*- 
from models import *
from forms import *
from django import forms
from django.template import RequestContext
from django.http import HttpResponse, HttpResponseRedirect
from django.template.loader import get_template
from django.core.paginator import Paginator
from django.core.urlresolvers import reverse
import datetime
from django.shortcuts import render_to_response
from django.utils import simplejson
from django.db import connection

def update_patient(request):
    if request.method == 'POST':
        name=request.POST.get('patient_name')
        age = request.POST.get('patient_age')
        tid = request.POST.get('patient_id')
        birthdate=request.POST.get('patient_birthdate')
        company=request.POST.get('patient_company')
        home=request.POST.get('patient_home')
        tel=request.POST.get('patient_tel')
        card=request.POST.get('patient_card')
        native=request.POST.get('patient_native')
        inputdate=request.POST.get('patient_inputdate')
        number=request.POST.get('patient_number')
        remark=request.POST.get('patient_remark')
        gender=request.POST.get('patient_gender')

        nno=request.POST.get('nno')
        nho=request.POST.get('nho')
        
        nnos=nno.split('-')
        nnhos=nho.split('-')
        if card == '':
                card = None
        
        npatient = patient.objects.get(id = tid)
        npatient.patient_name=name
        npatient.patient_age=age
        npatient.patient_birthdate=birthdate
        npatient.patient_company=company
        npatient.patient_home=home
        npatient.patient_tel=tel
        npatient.patient_card=card
        npatient.patient_native=native
        npatient.patient_inputdate=inputdate
        npatient.patient_number=number
        npatient.patient_remark=remark
        npatient.patient_gender=gender
 
        npatient.save()
        print connection.queries
        
        PatientNo.objects.filter(patient=npatient).delete()
        hospitalized.objects.filter(patient=npatient).delete()
        for thisnno in nnos:
            print thisnno+'\n'
            newPatientNo = PatientNo(Patient_no=thisnno,
                                  patient=npatient)
            newPatientNo.save()
            print connection.queries
        for thisnho in nnhos:
            print thisnho+'\n'
            newhospitalized = hospitalized(hospitalized_no=thisnho,
                                        patient=npatient)
            newhospitalized.save()
            print connection.queries
        
        flag = 1
        result = simplejson.dumps(flag)
        return HttpResponse(result)
    else:
        flag = 0
    result = simplejson.dumps(flag)
    return HttpResponse(result)


def update_Infection(request):
    if request.method == 'POST':
        Infection = request.POST.get('Infection','')
        Pregnancy = request.POST.get('Pregnancy','')
        Drugs = request.POST.get('Drugs','')
        Tired = request.POST.get('Tired','')
        Others = request.POST.get('Others','')
        Fitment = request.POST.get('Fitment','')
        Psychogenic = request.POST.get('Psychogenic','')
        incentive_id = request.POST.get('incentive_id','')
        
        nInfection = Incentive.objects.get(id = incentive_id)
        nInfection.Infection = Infection
        nInfection.Pregnancy = Pregnancy
        nInfection.Drugs = Drugs
        nInfection.Tired = Tired
        nInfection.Others = Others
        nInfection.Fitment = Fitment
        nInfection.Psychogenic = Psychogenic
        
        nInfection.save()
        print connection.queries
        flag = 1
        result = simplejson.dumps(flag)
        return HttpResponse(result)
    else:
        flag = 0
    result = simplejson.dumps(flag)
    return HttpResponse(result)
        
def update_Manifestation(request):
    if request.method == 'POST':
        Skin = request.POST.get('Skin')
        Joint = request.POST.get('Joint')
        Muscle = request.POST.get('Muscle')
        Respiratory = request.POST.get('Respiratory')
        Cardiovascular = request.POST.get('Cardiovascular')
        Digestive = request.POST.get('Digestive')
        Hemotologic = request.POST.get('Hemotologic')
        Urinary = request.POST.get('Urinary')
        Nervous = request.POST.get('Nervous')
        Initial = request.POST.get('Initial')
        Raynaud = request.POST.get('Raynaud')
        Menstruation = request.POST.get('Menstruation')
        
        manifestation_id = request.POST.get('manifestation_id')
        
        nManifestation = Manifestation.objects.get(id = manifestation_id)
        nManifestation.Skin = Skin
        nManifestation.Joint = Joint
        nManifestation.Muscle = Muscle
        nManifestation.Respiratory = Respiratory
        nManifestation.Cardiovascular = Cardiovascular
        nManifestation.Digestive = Digestive
        nManifestation.Hemotologic = Hemotologic
        nManifestation.Urinary = Urinary
        nManifestation.Nervous = Nervous
        nManifestation.Initial = Initial
        nManifestation.Raynaud = Raynaud
        nManifestation.Menstruation = Menstruation
        
        nManifestation.save()
        print connection.queries
        flag = 1
        result = simplejson.dumps(flag)
        return HttpResponse(result)
    else:
        flag = 0
    result = simplejson.dumps(flag)
    return HttpResponse(result)
            

def update_Course(request):
    if request.method == 'POST':
        tCourse = request.POST.get('Course')
        tCourse_id = request.POST.get('course_id')
        
        ncourse = Course.objects.get(id = tCourse_id)
        ncourse.Course = tCourse
        ncourse.save()
        print connection.queries
        flag = 1
        result = simplejson.dumps(flag)
        return HttpResponse(result)
    else:
        flag = 0
    result = simplejson.dumps(flag)
    return HttpResponse(result)
    
def update_History(request):
    if request.method == 'POST':
        Past = request.POST.get('Past')
        Family = request.POST.get('Family')
        Birth = request.POST.get('Birth')
        Pregnancy = request.POST.get('Pregnancy')
        Birthtime = request.POST.get('Birthtime')
        Abortion = request.POST.get('Abortion')
        Living = request.POST.get('Living')
        Fs = request.POST.get('Fs')
        Ms = request.POST.get('Ms')
        history_id = request.POST.get('history_id')
        
        nhistory = History.objects.get(id=history_id)
        
        nhistory.Past = Past
        nhistory.Family = Family
        nhistory.Birth = Birth
        nhistory.Pregnancy = Pregnancy
        nhistory.Birthtime = Birthtime
        nhistory.Abortion = Abortion
        nhistory.Living = Living
        nhistory.Fs = Fs
        nhistory.Ms = Ms
        
        nhistory.save()
        print connection.queries
        flag = 1
        result = simplejson.dumps(flag)
        return HttpResponse(result)
    else:
        flag = 0
    result = simplejson.dumps(flag)
    return HttpResponse(result)

def update_Physical(request):
    if request.method=='POST':
        Lung = request.POST.get('Lung')
        Heart = request.POST.get('Heart')
        Liver = request.POST.get('Liver')
        Spleen = request.POST.get('Spleen')
        Hair = request.POST.get('Hair')
        Skin2 = request.POST.get('Skin2')
        Joint = request.POST.get('Joint')
        Muscle2 = request.POST.get('Muscle2')
        Vasculitis = request.POST.get('Vasculitis')
        Lymph = request.POST.get('Lymph')
        Nervous2 = request.POST.get('Nervous2')
        physical_id = request.POST.get('physical_id')
        
        npphysical = Physical.objects.get(id = physical_id)
        
        npphysical.Lung = Lung
        npphysical.Heart = Heart
        npphysical.Liver = Liver
        npphysical.Spleen = Spleen
        npphysical.Hair = Hair
        npphysical.Skin2 = Skin2
        npphysical.Joint = Joint
        npphysical.Muscle2 = Muscle2
        npphysical.Vasculitis = Vasculitis
        npphysical.Lymph = Lymph
        npphysical.Nervous2 = Nervous2
        
        npphysical.save()
        print connection.queries
        flag = 1
        result = simplejson.dumps(flag)
        return HttpResponse(result)
    else:
        flag = 0
    result = simplejson.dumps(flag)
    return HttpResponse(result)
        
        
def update_Standard(request):
    if request.method == 'POST':
        optionsRadios1 = request.POST.get('optionsRadios1')
        optionsRadios2 = request.POST.get('optionsRadios2')
        optionsRadios3 = request.POST.get('optionsRadios3')
        optionsRadios4 = request.POST.get('optionsRadios4')
        optionsRadios5 = request.POST.get('optionsRadios5')
        optionsRadios6 = request.POST.get('optionsRadios6')
        optionsRadios7 = request.POST.get('optionsRadios7')
        optionsRadios8 = request.POST.get('optionsRadios8')
        optionsRadios9 = request.POST.get('optionsRadios9')
        optionsRadios10 = request.POST.get('optionsRadios10')
        optionsRadios11 = request.POST.get('optionsRadios11')
        optionsRadios12 = request.POST.get('optionsRadios12')
        optionsRadios13 = request.POST.get('optionsRadios13')
        optionsRadios14 = request.POST.get('optionsRadios14')
        optionsRadios15 = request.POST.get('optionsRadios15')
        optionsRadios16 = request.POST.get('optionsRadios16')
        optionsRadios17 = request.POST.get('optionsRadios17')
        Standard_id = request.POST.get('Standard_id')
        
        nStandard = Standard.objects.get(id=Standard_id)
        
        nStandard.optionsRadios1 = optionsRadios1
        nStandard.optionsRadios2 = optionsRadios2
        nStandard.optionsRadios3 = optionsRadios3
        nStandard.optionsRadios4 = optionsRadios4
        nStandard.optionsRadios5 = optionsRadios5
        nStandard.optionsRadios6 = optionsRadios6
        nStandard.optionsRadios7 = optionsRadios7
        nStandard.optionsRadios8 = optionsRadios8
        nStandard.optionsRadios9 = optionsRadios9
        nStandard.optionsRadios10 = optionsRadios10
        nStandard.optionsRadios11 = optionsRadios11
        nStandard.optionsRadios12 = optionsRadios12
        nStandard.optionsRadios13 = optionsRadios13
        nStandard.optionsRadios14 = optionsRadios14
        nStandard.optionsRadios15 = optionsRadios15
        nStandard.optionsRadios16 = optionsRadios16
        nStandard.optionsRadios17 = optionsRadios17
        
        nStandard.save()
        print connection.queries
        flag = 1
        result = simplejson.dumps(flag)
        return HttpResponse(result)
    else:
        flag = 0
    result = simplejson.dumps(flag)
    return HttpResponse(result)
    
        
        

    
    